# -*- coding:utf8 -*-
import itchat,re
import MySQLdb,time
import urllib
import urllib2
from wxpy import *
itchat.auto_login()
# itchat.send('Hello, filehelper', toUserName='filehelper')
#  获取自己的用户信息，返回自己的属性字典
friends = itchat.get_friends(update = True)[0:]
for i in  range (0,len(friends)):
       friend = friends[i]
       print friend
       NickName = friend['NickName']
       print NickName
       UserName = friend['UserName']
       print UserName
       Sex = friend['Sex']
       print Sex
       Province = friend['Province']
       print Province
       City = friend['City']
       print City
       Signature = friend['Signature']
       print Signature
       conn = MySQLdb.connect(host='127.0.0.1', user='root', passwd='', charset="utf8", use_unicode=False)  # 连接服务器
       cur = conn.cursor()
       sql = "insert into weixin.test(NickName,Sex,Province,City,Signature) values(%s,%s,%s,%s,%s)"
       param = (NickName,Sex,Province,City,Signature)
       try:
           A = cur.execute(sql, param)
           conn.commit()
       except Exception as e:
           print(e)
           conn.rollback()


# # 获取特定UserName的用户信息
# itchat.search_friends(userName='@abcdefg1234567')
# # 获取分别对应相应键值的用户
# itchat.search_friends(wechatAccount='littlecodersh')
# # 三、四项功能可以一同使用
# itchat.search_friends(name='LittleCoder机器人', wechatAccount='littlecodersh')